﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace LIK.NetAssemblyChecker
{
    public partial class frmAssemblyBrowser : Form
    {

        public frmAssemblyBrowser(object  a)
        {
            InitializeComponent();
            if (a is Assembly)
            {
                foreach (string ns in GetAssemblyNamespaces(((Assembly)a)).Values)
                {
                    TreeNode nodeNamespace = trvFileAssembly.Nodes.Add(ns);
                    //nodeNamespace.ImageKey = nodeNamespace.SelectedImageKey = "namespace.ico";
                    TreeNode dummy = nodeNamespace.Nodes.Add("");
                  }
            }
        }

        private Dictionary<string, Assembly> _FileAssemblies = new Dictionary<string, Assembly>();

        public Dictionary<string, string> GetAssemblyNamespaces(Assembly fileAssembly)
        {
            Dictionary<string, string> namespaces = new Dictionary<string, string>();
            if (fileAssembly == null)
                foreach (Assembly assembly in _FileAssemblies.Values)
                    foreach (Type type in assembly.GetExportedTypes())
                    {
                        string ns = (type.Namespace == null ? "(none)" : type.Namespace);
                        if (ThereisAccessibleType(type) && !namespaces.ContainsKey(ns))
                            namespaces.Add(ns, ns);
                    }
            else
                foreach (Type type in fileAssembly.GetExportedTypes())
                {
                    string ns = (type.Namespace == null ? "(none)" : type.Namespace);
                    if (ThereisAccessibleType(type) && !namespaces.ContainsKey(ns))
                        namespaces.Add(ns, ns);
                }
            return namespaces;
        }

        public bool ThereisAccessibleType(Type type)
        {
            return (!type.IsGenericType && !type.IsInterface && !IsPrimitive(type)
                && !IsDelegate(type) && !IsEventArgs(type)
                && !IsException(type) && !IsAttribute(type));
        }


        // Helper functions for a type
        public bool IsPrimitive(Type type)
        {
            return (type.IsPrimitive || type.IsEnum
                || typeof(string).Equals(type));
        }
        public bool IsDelegate(Type type)
        {
            return InheritsFrom(type, typeof(System.Delegate));
        }
        public bool IsException(Type type)
        {
            return InheritsFrom(type, typeof(System.Exception));
        }
        public bool IsEventArgs(Type type)
        {
            return InheritsFrom(type, typeof(System.EventArgs));
        }
        public bool IsAttribute(Type type)
        {
            return InheritsFrom(type, typeof(System.Attribute));
        }
        public bool InheritsFrom(Type type, Type baseType)
        {
            return baseType.IsAssignableFrom(type);
        }

    }
}
